<?php
	 namespace Api\Controller;
	 use Think\Controller;
	 require_once './qiniu/autoload.php';
	 use Qiniu\Auth;
	 use Qiniu\Storage\UploadManager;
	 
	 use Qiniu\Storage\BucketManager;
	 header("Content-Type:text/html;charset=utf-8");
	 class UploadheadimgController extends Controller
	 {
	 	public function uploadHeadimg()
	 	{
			
		 	$setting=C('UPLOAD_SITEIMG_QINIU');
			$Upload=new \Think\Upload\Driver\Qiniu\QiniuStorage($setting);
			$token = $Upload -> upload($setting, $file);
	 		var_dump($token);
	 		var_dump("./Public/Uploads/Headimg/05426a777c6415ca45be09f0af701a9f.jpg");
	 		
	 		
	 	}
	 	public function ImgUpload()
	 	{
	 		//$this->error("没有文件！");
	 		//TODO: 用户登录检测
	 		/* 调用文件上传组件上传文件 */
	 		$Picture = D('Picture');
	 		$pic_driver = C('PICTURE_UPLOAD_DRIVER');
	 		$info = $Picture->upload(
	 				$_FILES,
	 				C('PICTURE_UPLOAD'),
	 				C('PICTURE_UPLOAD_DRIVER'),
	 				C("UPLOAD_&#123;&#36;pic_driver&#125;_CONFIG")
	 		); //TODO:上传到远程服务器
	 		/* 记录图片信息 */
	 		if($info){
	 			/* 返回JSON数据 */
	 			echo json_encode($info);
	 	
	 		} else {
	 			echo json_encode($info);
	 		}
	 	}
	 	public function ImgUp()
	 	{
	 		var_dump(I());
	 		/* $userinfo = M("User") -> where("uid = {$_POST['uid']}") ->find(); */
	 		/* if ($userinfo['token'] != $_POST['token'])
	 		 {
	 		$array['result'] = -2;
	 		$array['message'] = "token过期或错误，请重新登录获取";
	 		echo json_encode($array, JSON_UNESCAPED_SLASHES);
	 		exit;
	 		} */
	 		$img = base64_decode($_REQUEST['headimg']);
	 		$oldname = $_REQUEST['oldname'];
	 		$index = strrpos($oldname, ".");//对文件名进行“.”截取
	 		$ext = substr($oldname, $index);//获得文件的扩展名
	 		$newname = md5(time() . rand(0, 9999)).$ext;
	 		$result = file_put_contents('http://oc169kdqd.bkt.clouddn.com/'.$newname, $img);
	 		var_dump($result);
	 		$data["headimg"] = "http://192.168.1.105/loveshow/Public/Uploads/Headimg/".$newname;
	 	}
	 
	 	public function unlinkImg()
	 	{
	 		$result = unlink("./Public/Uploads/Headimg/05426a777c6415ca45be09f0af701a9f.jpg");
	 		var_dump($result);
	 	}
	 	//上传写真
	 	public function uploadMirror()
	 	{
// 	 		var_dump($_FILES['headimg'])
	 		
	 		$bucket = 'show';
	 		$accessKey = '-JLvYg-7Pa1SGcLzw1qyrzSt5dqpp-7Xr5ZB_XI3';
	 		$secretKey = 'p0srnaa2qyYM7fS5kKp_OCUTYwiUMb-Jr8C9VXxY';
	 		$auth = new Auth($accessKey, $secretKey);
	 		$upToken = $auth->uploadToken($bucket, null, 3600);//获取上传所需的token
	 		$uploadMgr = new UploadManager();
	 		
	 		
	 		// 上传文件到七牛
// 	 		$filePath = file_get_contents($_FILES["tmp_name"]);
// 	 		header("content-type:image/jpeg");
// 	 		echo $filePath;exit;
	 		$oldname = $_POST['name'];
	 		$index = strrpos($oldname, ".");//对文件名进行“.”截取
	 		$ext = substr($oldname, $index);//获得文件的扩展名
	 		$key = md5(time() . rand(0, 9999)).$ext;
	 		/* $response = $uploadMgr->putFile($upToken, $key, $filePath); */
	 		
	 		
	 		$remote_server = 'http://upload.qiniu.com/putb64/-1';
	 		$post_string = file_get_contents($_FILES['headimg']['tmp_name']);
	 		$data = array();
	 		
	 		function request_by_curl($remote_server,$post_string,$upToken) 
	 		{
	 		
	 			$headers = array();
	 			$headers[] = 'Content-Type:image/png';
	 			$headers[] = 'Authorization:UpToken '.$upToken;
	 			$ch = curl_init();
	 			curl_setopt($ch, CURLOPT_URL,$remote_server);
	 			//curl_setopt($ch, CURLOPT_HEADER, 0);
	 			curl_setopt($ch, CURLOPT_HTTPHEADER ,$headers);
	 			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	 			//curl_setopt($ch, CURLOPT_POST, 1);
	 			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
	 			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
	 			curl_setopt($ch, CURLOPT_TIMEOUT, 30);
	 			$data = curl_exec($ch);
	 			curl_close($ch);
	 			return $data;
	 			
	 		}
	 		
	 		$tempData = request_by_curl($remote_server, $post_string, $upToken);
	 		
	 		file_put_contents('text.log', var_export($tempData,true));
	 		
	 		/* if ($data == null) {
	 		
	 			$array1['result'] = -1;
	 			$array1['message'] = "图片上传失败！";
	 			echo json_encode($array1, JSON_UNESCAPED_SLASHES);
	 			exit;
	 		}else{
	 			$array1['result'] = 0;
	 			$array1['message'] = "头像上传成功！";
	 			$array1['data'] = $data;
	 			echo json_encode($array1, JSON_UNESCAPED_SLASHES);
	 			exit;
	 		
	 		} */  
	 		
	 		/*function request_by_curl($remote_server,$post_string,$upToken) {
	 		
	 			$headers = array();
	 			$headers[] = 'Content-Type:image/png';
	 			$headers[] = 'Authorization:UpToken '.$upToken;
	 			$ch = curl_init();
	 			curl_setopt($ch, CURLOPT_URL,$remote_server);
	 			//curl_setopt($ch, CURLOPT_HEADER, 0);
	 			curl_setopt($ch, CURLOPT_HTTPHEADER ,$headers);
	 			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	 			//curl_setopt($ch, CURLOPT_POST, 1);
	 			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
	 			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
	 			curl_setopt($ch, CURLOPT_TIMEOUT, 30);
	 			$data = curl_exec($ch);
	 			curl_close($ch);
	 		
	 			return $data;
	 		}
	 		$str=$_FILES['headimg'];
	 		echo "<pre>";
	 		echo request_by_curl('http://upload.qiniu.com/putb64/-1',$str,$upToken);
	 		echo "</pre>";
	 		var_dump(request_by_curl('http://upload.qiniu.com/putb64/-1',$str,$upToken)); */
	 	}
	 	//向前端传送token值
	 	public function uploadToken()
	 	{
	 		$bucket='show';
	 		$expires = 3600;
	 		$accessKey='-JLvYg-7Pa1SGcLzw1qyrzSt5dqpp-7Xr5ZB_XI3';
	 		$secretKey='p0srnaa2qyYM7fS5kKp_OCUTYwiUMb-Jr8C9VXxY';
	 		$auth = new Auth($accessKey, $secretKey);
	 		$upToken = $auth->uploadToken($bucket, null, time()+3600);//获取上传所需的token
	 		
	 		
	 		var_dump($upToken);
	 		/* $client = new QiniuClient($accessKey,$secretKey);
	 		$flags = array();
	 		$scope = $bucket;
	 		$deadline = time() + $expires;
	 		$flags['scope'] = $scope;
	 		$flags['deadline'] = $deadline;
	 		$flags['returnBody'] = null;
	 		echo $client->uploadToken($flags); */
	 	}
	 	/* 
	 	 *通告列表显示 
	 	 *  
	 	 */
	 	public function announceInfo($page=null)
	 	{
	 		$currentpage = isset($page) ? $page:1;
	 			
	 		$pagesize    = 10;
	 		$start       = ($currentpage-1)*$pagesize;// 分页显示输出
	 			
	 		$announceInfo = M("Annunciate") -> limit($start , $pagesize)  -> select();
	 		foreach ($announceInfo as $key=>$value)
	 		{
	 			$data = array();
	 			$data = array(
	 					'title'=>$value['annunciate_r'],
	 					'picture'=>"http://192.168.1.105/loveshow/Public/Uploads/Images/".$value['annunciate_p'],
	 					'content'=>$value['annunciate_w'],
	 					'time'=>$value['time']
	 			);
	 			$announceInfo[$key] = $data;
	 		}
	 		if ($announceInfo != null)
	 		{
	 			$array['result'] = 0;
	 			$array['message'] = "通告信息获取成功！";
	 			$array['list'] = $announceInfo;
	 			echo json_encode($array, JSON_UNESCAPED_SLASHES);
	 			exit;
	 		}
	 		else
	 		{
	 			$array['result'] = -1;
	 			$array['message'] = "通告信息获取失败！";
	 			echo json_encode($array, JSON_UNESCAPED_SLASHES);
	 			exit;
	 		}
	 	}
	 	/*
	 	 *轮播图 
	 	 *   
	 	 */ 
	 	public function carouselImg()
	 	{
	 		$carouselimg = M("Carouselimg") -> select();
	 			
	 		foreach ($carouselimg as $key=>$value)
	 		{
	 			$data = array();
// 	 			if ($value['id'] == null)
// 	 			{
// 	 				$id = "";
// 	 			}
// 	 			if ($value['url'] == null)
// 	 			{
// 	 				$url = "";
// 	 			}
	 	
	 			$data = array(
	 					'img'=>"http://192.168.1.105/loveshow/Public/Uploads/Carouselimg/".$value['cimg'],
	 					'type'=>$value['type'],
	 					'id'=>$value['id'],
	 					'url'=>$value['url']
	 			);
	 			$carouselimg[$key] = $data;
	 		}
	 		var_dump($carouselimg);
// 	 		if ($carouselimg != null)
// 	 		{
// 	 			$array['result'] = 0;
// 	 			$array['message'] = "图片信息获取成功！";
// 	 			$array['list'] = $carouselimg;
// 	 			echo json_encode($array, JSON_UNESCAPED_SLASHES);
// 	 			exit;
// 	 		}
// 	 		else
// 	 		{
// 	 			$array['result'] = -1;
// 	 			$array['message'] = "图片信息获取失败！";
// 	 			echo json_encode($array, JSON_UNESCAPED_SLASHES);
// 	 			exit;
// 	 		}
	 	}
	 	
	 }
	 